package com.bgrc.hubu12305.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgrc.hubu12305.common.CodeEnum;
import com.bgrc.hubu12305.common.Result;
import com.bgrc.hubu12305.entity.ParkingStationEntity;
import com.bgrc.hubu12305.entity.TrainInfoEntity;
import com.bgrc.hubu12305.service.ParkStationVOService;
import com.bgrc.hubu12305.service.ParkingStationService;
import com.bgrc.hubu12305.service.TrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 包名:com.bgrc.hubu12305.controller
 * 用户:86182
 * 时间:20211216:032021/12/17
 */
@RestController
@RequestMapping("admin")
@Api(tags = "管理员接口")
public class AdminController {

    @Autowired
    private TrainService trainService;

    @Autowired
    private ParkingStationService parkingStationService;

    //添加车次
    @ApiOperation(value = "添加车次")
    @PostMapping("/saveTrain")
    public Result saveTrain(@RequestBody TrainInfoEntity trainInfoEntity){
        Integer exist = trainService.isExist(trainInfoEntity.getTrainNo());
        if (exist>0){
            return Result.build(null,CodeEnum.TRAIN_EXIST);
        }
        boolean b = trainService.save(trainInfoEntity);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    //通过车次删除,逻辑删除
    @ApiOperation(value = "删除车次")
    @GetMapping("/deletedTrain")
    public Result deletedTrain(@RequestParam String trainNo){
        boolean b = trainService.removeById(trainNo);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    //修改车次
    @ApiOperation("修改车次")
    @PostMapping("updateTrain")
    public Result updateTrain(@RequestBody TrainInfoEntity trainInfoEntity){

        boolean b = trainService.updateById(trainInfoEntity);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
    @ApiOperation(value = "批量删除车次")
    @GetMapping("/deletedByIds")
    public Result deletedByIds(@RequestParam ArrayList<String> ids){
        boolean b = trainService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    //添加经停站
    @ApiOperation(value = "添加经停站")
    @PostMapping("/saveParkStation")
    public Result saveParkStation(@RequestBody ParkingStationEntity parkingStationEntity){

        String stationNo = parkingStationEntity.getStationNo();
        String trainNo = parkingStationEntity.getTrainNo();
        Integer exist = parkingStationService.isExist(trainNo, stationNo);

        if (exist>0){
            return Result.build(null,CodeEnum.STATION_EXIST);
        }
        boolean save = parkingStationService.save(parkingStationEntity);
        System.out.println(save);
        if (save){
            return Result.ok();
        }
        return Result.fail();
    }

    //根据车次和站名删除经停站，物理删除
    @ApiOperation(value = "删除经停站")
    @PostMapping("/deleteParkStation")
    public Result deleteParkStation(@RequestParam String trainNumber,
                                    @RequestParam String stationName){
        QueryWrapper<ParkingStationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_number",trainNumber);
        queryWrapper.eq("station_name",stationName);
        boolean remove = parkingStationService.remove(queryWrapper);
        if (remove){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "修改经停站信息")
    @PostMapping("/updateParkStation")
    public Result updateParkStation(@RequestBody ParkingStationEntity parkingStationEntity){

        String stationNo = parkingStationEntity.getStationNo();
        String trainNo = parkingStationEntity.getTrainNo();

        Integer exist = parkingStationService.isExist(trainNo, stationNo);
        if (!(exist>0)){
            return Result.build(null,CodeEnum.STATION_INEXISTENT);
        }
        QueryWrapper<ParkingStationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_no",parkingStationEntity.getTrainNo());
        queryWrapper.eq("station_no",parkingStationEntity.getStationNo());
        boolean b = parkingStationService.update(parkingStationEntity,queryWrapper);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
    @ApiOperation("根据车次查询经停站")
    @GetMapping("/queryParkStationByTrainNumber")
    public Result queryParkStationByTrainNumber(@RequestParam String trainNumber){
        QueryWrapper<ParkingStationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_number",trainNumber);
        List<ParkingStationEntity> stationList = parkingStationService.list(queryWrapper);
        return Result.ok(stationList);
    }
}
